package controller.admin;

import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminUserDelete", value = "/admin/user_delete")
public class AdminUserDelete extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserService us = new UserService();
        String id = request.getParameter("id");
        if (!(id != null && !"".equals(id))) {
            request.getRequestDispatcher("user_list").forward(request, response);
            return;
        }

        boolean flag = us.deleteUser(Integer.parseInt(id));
        if (flag) {
            request.setAttribute("msg", "删除成功！");
        } else {
            request.setAttribute("failMsg", "删除失败，可能该用户下了订单！");
        }
        request.getRequestDispatcher("user_list").forward(request, response);
    }
}
